#include <vector>
#include <functional>
#include "RoomMachine.h"
#include "../Context.h"
#include "../Action.h"

// 初始化, 事件绑定动作, 状态保定事件
void RoomMachine::Init()
{
    m_stateEventsMap.clear();
    
    std::vector<std::shared_ptr<State>> states; states.clear();

    std::shared_ptr<State> emptyState = std::make_shared<UserState>(Empty);
    std::shared_ptr<State> hasUserState = std::make_shared<UserState>(HasUser);

    states.push_back(emptyState);
    states.push_back(hasUserState);

    for(int i = 0; i < states.size(); i++)
    {
        EventAction eventActionMap; eventActionMap.clear();
        m_stateEventsMap.insert(std::make_pair(states[i]->m_stateId, eventActionMap));
    }
    

    std::function<bool(std::shared_ptr<Context>)> checkUserCountM0Func = std::bind(&RoomMachine::CheckUserCountM0, this, std::placeholders::_1);
    std::function<bool(std::shared_ptr<Context>)> checkUserCountL0Func = std::bind(&RoomMachine::CheckUserCountL0, this, std::placeholders::_1);
    
    std::shared_ptr<Action> hasUser_Empty_Action = std::make_shared<Room_LeaveGameRoomAction>(HasUser, Empty, checkUserCountL0Func);
    std::shared_ptr<Action> empty_HasUser_Action = std::make_shared<Room_EnterGameRoomAction>(Empty, HasUser, checkUserCountM0Func);

    // 没人状态 -----按下进入游戏房间按钮 + 判断人数 -------> 有人状态
    m_stateEventsMap[emptyState->m_stateId].insert(std::make_pair(PressEnterGameRoom201Btn, empty_HasUser_Action));
    m_stateEventsMap[emptyState->m_stateId].insert(std::make_pair(PressEnterGameRoom202Btn, empty_HasUser_Action));

    // 没人状态 -----按下退出游戏房间按钮 + 判断人数 -------> 无人状态
    m_stateEventsMap[emptyState->m_stateId].insert(std::make_pair(PressLeaveGameRoom201Btn, empty_HasUser_Action));
    m_stateEventsMap[emptyState->m_stateId].insert(std::make_pair(PressLeaveGameRoom202Btn, empty_HasUser_Action));

    // 有人状态 -----按下进入游戏房间按钮 + 判断人数 -------> 有人状态
    m_stateEventsMap[hasUserState->m_stateId].insert(std::make_pair(PressEnterGameRoom201Btn, hasUser_Empty_Action));
    m_stateEventsMap[hasUserState->m_stateId].insert(std::make_pair(PressEnterGameRoom202Btn, hasUser_Empty_Action));

    // 有人状态 -----按下退出游戏房间按钮 + 判断人数 -------> 无人状态
    m_stateEventsMap[hasUserState->m_stateId].insert(std::make_pair(PressLeaveGameRoom201Btn, hasUser_Empty_Action));
    m_stateEventsMap[hasUserState->m_stateId].insert(std::make_pair(PressLeaveGameRoom202Btn, hasUser_Empty_Action));
}

bool RoomMachine::CheckUserCountM0(std::shared_ptr<Context> context)
{
    if(context->m_userCount >= 0){
        return true;
    }
    return false;
}

bool RoomMachine::CheckUserCountL0(std::shared_ptr<Context> context)
{
    if(context->m_userCount <= 0){
        return true;
    }
    return false;
}